package com.demo.blog.controller;

import com.demo.blog.model.Result;
import com.demo.blog.model.User;
import com.demo.blog.service.CaptchaService;
import com.demo.blog.service.UserService;
import com.demo.blog.utils.JwtUtils;
import com.demo.blog.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-07-24
 * Time: 14:23
 */

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private CaptchaService captchaService;

    @RequestMapping("/login")
    public Result<Object> login(String userName, String password, String user_captcha, HttpSession session) {
        /**
         * 1. 参数校验
         * 2. 密码校验
         * 3. 生成token, 并返回
         */

        if (!StringUtils.hasLength(user_captcha)) {
            return Result.fail("验证码为空");
        }


        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return Result.fail("用户名或密码为空");
        }

        if (!captchaService.check(user_captcha, session)) {
            return Result.fail("验证码错误");
        }

        //获取数据库中的密码
        User userInfo = userService.queryByName(userName);
        if (userInfo == null || userInfo.getId() < 0) {
            return Result.fail("用户不存在");
        }
//        if (!password.equals(userInfo.getPassword())){
//            return Result.fail("密码错误!");
//        }
        if (!SecurityUtils.verify(password, userInfo.getPassword())) {
            return Result.fail("密码错误!");
        }
        //生成token, 并返回
        Map<String, Object> claim = new HashMap<>();
        claim.put("id", userInfo.getId());
        claim.put("name", userInfo.getUserName());
        String token = JwtUtils.genToken(claim);
        return Result.success(token);
    }

    /**
     * 获取当前作者的信息
     * 如果当前⻚⾯是博客列表⻚, 则显⽰当前登陆⽤⼾的信息.
     */
    @RequestMapping("/getUserInfo")
    public User getUserInfo(HttpServletRequest request) {
        //1. 从token中获取用户ID
        //2. 根据用户ID, 获取用户信息
        String token = request.getHeader("user_token");
        Integer userId = JwtUtils.getUserIdFromToken(token);
        if (userId == null) {
            return null;
        }
        return userService.queryById(userId);
    }

    /**
     * 获取当前作者的信息
     * 如果当前⻚⾯是博客详情⻚, 则显⽰该博客的作者⽤⼾信息.
     */
    @RequestMapping("/getAuthorInfo")
    public User getAuthorInfo(Integer blogId) {
        if (blogId == null && blogId < 0) {
            return null;
        }
        return userService.getAuthorInfo(blogId);
    }

    @RequestMapping("/register")

    public Result<Object> registerUser(String userName, String password, String true_password, String user_captcha, HttpSession session) {
        if (!StringUtils.hasLength(user_captcha)) {
            return Result.fail("验证码为空");
        }
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)||!StringUtils.hasLength(true_password)) {
            return Result.fail("用户名或密码为空");
        }

        if (!captchaService.check(user_captcha, session)) {
            return Result.fail("验证码错误");
        }

        if (!password.equals(true_password)) {
            return Result.fail("两次密码不同");
        }
        //
        User userInfo = userService.queryByName(userName);
        if (userInfo != null && userInfo.getId() >= 0) {
            return Result.fail("用户已存在");
        }
        userService.insertUser(userName,password);

        return Result.success(true);
    }
}
